package com.attendance.service.impl;


import com.attendance.common.CommonPage;
import com.attendance.common.CommonResult;
import com.attendance.mapper.ApartMajorGradeMapper;
import com.attendance.mapper.ClassMapper;
import com.attendance.pojo.Class;
import com.attendance.pojo.Pages;
import com.attendance.pojo.Teacher;
import com.attendance.service.ClassService;
import com.attendance.service.TeacherService;
import com.attendance.vo.ClassVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service
public class  ClassServiceImpl implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private TeacherService teacherService;

    @Override
    public CommonResult selectBy(ClassVO classVO, Pages pages) {
        if (classVO.getTeaName()!=null){
            //获取教师id
            Teacher teacher = teacherService.selectByTeaName(classVO.getTeaName());
            if (teacher==null){
                return CommonResult.failed("没有数据");
            }else {
                classVO.setTeaId(teacher.getTeaId());
                //获取班级数据
                PageHelper.startPage(pages.getPage(),pages.getNum());//分页
                return  CommonResult.success(CommonPage.restPage(classMapper.selectClass(classVO)));
            }
        }else {
            PageHelper.startPage(pages.getPage(),pages.getNum());//分页
            return  CommonResult.success(CommonPage.restPage(classMapper.selectClass(classVO)));
        }
    }

    @Override
    public CommonResult addClass(ClassVO classVO) {
        int i = classMapper.addClass(classVO);
        if (i==1){
            return CommonResult.success("添加成功");
        }else {
            return  CommonResult.failed("添加失败");
        }
    }

    @Override
    public CommonResult updateClass(ClassVO classVO) {
        int i = classMapper.updateClass(classVO);
        if (i==1){
            return CommonResult.success("修改成功");
        }else {
            return  CommonResult.failed("修改失败");
        }
    }

    @Override
    public CommonResult deleteClass(ClassVO classVO) {
        int i = classMapper.deleteClass(classVO);
        if (i==1){
            return CommonResult.success("删除成功");
        }else {
            return  CommonResult.failed("删除失败");
        }
    }
}
